package com.lagou.rocketmq.interceptor;

import com.lagou.rocketmq.mapper.GoodsMapper;
import com.lagou.rocketmq.pojo.Goods;
import com.lagou.rocketmq.pojo.GoodsExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

@Component

public class MyInterceptor implements HandlerInterceptor {



    @Autowired
    GoodsMapper goodsMapper;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String goodsName = request.getParameter("goodsName");
        GoodsExample example = new GoodsExample();
        example.createCriteria().andNameEqualTo(goodsName);
        List<Goods> goods = goodsMapper.selectByExample(example);
        if(goods != null && goods.size()>0){
            Goods goodsInfo = goods.get(0);
            if(goodsInfo.getSecKillTime().compareTo(new Date())<0){
                //已到秒杀时间
                return true;
            }
            System.out.println("还未到秒杀开始时间");
        }
        return false;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }
}
